<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;

use App\Model\AdminMenuModel;
use App\Model\LazyCommon\AdminLogModel;
use App\Model\LazyCommon\CCM;
use Symfony\Component\HttpFoundation\Request;

class BaseController4Admin extends BaseController {


    public $data;
    public $theme = "Sano";

    public function __construct(FormCreator $formCreator)
    {
        parent::__construct();
        $this->formCreator = $formCreator;
    }

    public function show($template,$data = [],$response = null)
    {
        $request = Request::createFromGlobals();
        $bc = new BaseCache();
        $menu = $bc->getItem("initMenu");
        $data["menu"] = $menu;

        //按钮功能应该在用户登录时确定
        $AdminMenuModel = new AdminMenuModel($this->getDoctrine());
        $menuTree = $AdminMenuModel->getRootAdminMenu();
        $data["menuTree"] = $menuTree;


        $assetsStr = "/themes/sano";
        $data["assetsRoot"] = $assetsStr;
        $data["assetsRoot"] = $assetsStr;
        $data["rnd"] = "?rnd=".time();
//        dd($request);
        $data["uri"] = $request->getPathInfo();
        return $this->render($template,$data,$response);
    }
    public function needToBeLogin()
    {
//        $url = $this->generateUrl("BackLogin");
//        header("location:$url");
//        die();

        header('content-type:application/json');
        header('charset:utf8');
        header("HTTP/1.1 203 Non-Authoritative Information");
        $s = 203;
        $i = "正在进行身份验证";
        $re = array(
            "message"    => "请登录后再操作",
            "data"    => [],
        );
        die(json_encode($re));
    }

    public function isAdmin()
    {
        $roles = $this->getSessionData("role");
        if (is_array($roles) && in_array("ROLE_ADMIN",$roles)) {
            return $this->getSessionData("id");
        } else {
            return false;
        }
    }

    public function needToBeAdmin()
    {
//        return 1;
        if ($this->isAdmin()) {
            return $this->getSessionData("id");
        } else {
            $this->needToBeLogin();
        }
    }



}
